﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using KOPLibrary.TemplateManagement;
using System.Security.Cryptography;
using System.IO;

namespace KOPLibrary.Forms
{
    public partial class OpenProtectedDocument : Form
    {
        public class OpenProtectDocumentResult
        {
            public string DocumentPath { get; set; }
            public string DocPwd { get; set; }
        }

        private TemplateType templateType { get; set; }
        private string selectedDoc = null;
        private string selectedKeyFile = null;
        private OpenProtectDocumentResult result = null;
        private void setEnabled()
        {
            ctlGo.Enabled = selectedKeyFile != null &&
                selectedDoc != null &&
                !string.IsNullOrEmpty(ctlPassword.Text);
        }

        public OpenProtectedDocument()
        {
            InitializeComponent();
        }

        private void OpenProtectedDocument_Load(object sender, EventArgs e)
        {
            setEnabled();
            ctlPassword.Focus();
        }

        public static OpenProtectDocumentResult ExecuteOpenProtectDocument(TemplateType type)
        {
            OpenProtectedDocument form = new OpenProtectedDocument();
            form.templateType = type;
            return form.Execute();
        }

        private OpenProtectDocumentResult Execute()
        {
            ctlSelectFile.Text = string.Format("{0} dokümanı seçin", templateType);
            if (ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                return result;
            }
            else return null;
        }



        private void ctlSelectEncFile_Click(object sender, EventArgs e)
        {
            if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                selectedKeyFile = ofd.FileName;
                ctlSelectEncFile.Text = "Seçildi";
                setEnabled();
                ctlPassword.Focus();
            }
        }

        private void ctlSelectFile_Click(object sender, EventArgs e)
        {
            if (ofdWord.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                selectedDoc = ofdWord.FileName;
                ctlSelectFile.Text = "Seçildi";
                setEnabled();
            }
        }

        private void ctlGo_Click(object sender, EventArgs e)
        {
            AesManaged aes = new AesManaged();
            Rfc2898DeriveBytes rfc = new Rfc2898DeriveBytes(ctlPassword.Text, Encoding.ASCII.GetBytes(ctlPassword.Text));
            aes.Key = rfc.GetBytes(32);
            aes.IV = Encoding.ASCII.GetBytes("0123456789ABCDRE");
            ICryptoTransform tr = aes.CreateDecryptor();
            MemoryStream ms = new MemoryStream();
            byte[] buffer = new byte[1024];
            using (FileStream f = new FileStream(selectedKeyFile, FileMode.Open))
            {
                using (CryptoStream cs = new CryptoStream(f, tr, CryptoStreamMode.Read))
                {
                    int read = 0;
                    try
                    {
                        while ((read = cs.Read(buffer, 0, buffer.Length)) > 0)
                            ms.Write(buffer, 0, read);
                    }
                    catch (CryptographicException)
                    {
                        Util.ShowError("Belirtilen şifre geçersizdir");
                        throw;
                    }

                    cs.Close();
                }
                f.Close();
            }
            ms.Seek(0, SeekOrigin.Begin);
            
            
            result = new OpenProtectDocumentResult() { DocPwd = Encoding.ASCII.GetString(ms.ToArray()), DocumentPath = selectedDoc };
            ms.Close();
            DialogResult = System.Windows.Forms.DialogResult.OK;
        }

        private void ctlPassword_TextChanged(object sender, EventArgs e)
        {
            setEnabled();
        }
    }
}
